package com.example.jucsample.cb;

import java.util.Random;
import java.util.concurrent.CyclicBarrier;

/**
 * @author zhenwei.wu
 * @date 2021/3/17
 */
public class Wangzhe {

    public static void main(String[] args) {
        // 一组线程均达到同步点后再执行
        int player = 10;
        CyclicBarrier cb = new CyclicBarrier(player);
        for (int i = 0; i < player; i++) {
            final int n = i + 1;
            new Thread(() -> {
                try {
                    Random random = new Random();
                    System.out.println(String.format("player-%s is choosing hero", n));
                    Thread.sleep(random.nextInt(8) * 1000);
                    System.out.println(String.format("player-%s has chosen hero", n));
                    cb.await();
                    System.out.println(String.format("player-%s loading", n));
                    Thread.sleep(random.nextInt(8) * 1000);
                    System.out.println(String.format("player-%s has loaded over", n));
                    cb.await();
                    System.out.println(String.format("player-%s start", n));
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }).start();
        }
    }

}
